home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12074 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.salford.ac.uk!aber!not-for-mail
  2. From: auj@aber.ac.uk (Alun "Da Penguin" Jones)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Determining the length of an int in string form
  5. Date: 28 Mar 1996 14:52:49 -0000
  6. Organization: Prifysgol Cymru (University of Wales) Aberystwyth
  7. Message-ID: <4je941$hup@osfa.aber.ac.uk>
  8. References: <3146D058.DD7@cbm.com> <4i7uth$qph@inet-nntp-gw-1.us.oracle.com> <DoE38u.GIH@iquest.net> <827163143snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: osfa.aber.ac.uk
  10.  
  11. Further up the screen fred@genesis.demon.co.uk wrote:
  12. >
  13. >>wkaufman@wkaufman.us.oracle.com (William Kaufman) wrote:
  14. >>+In article <3146D058.DD7@cbm.com> Dave Payne <paynedc@cbm.com> writes:
  15. >>+] Consider this:
  16. >>+]
  17. >>+] I have a variable of type int, and I would like to use the sprintf()
  18. >>+] function to write this variable to a string.  However, I want to
  19. >>+] dynamically allocate the space for the string, and only malloc enough
  20. >>+] space to hold the int.
  21. >>
  22. >>WHY ???
  23. >>
  24. >>Even a 32-bit int is only 9 characters at most, and that's *with* the sign. 
  25. >
  26. >-2147483647 is a valid 32 bit int and is 11 characters (excluding the null
  27. >character string terminator). The problem of course is that ints aren't
  28. >limited to 32 bits - the language imposes no upper limit to their size.
  29.  
  30. True... If you're working in 2's complement, then you should be able to
  31. calculate the maximum, including the sign and null as follows:
  32.  
  33. (int) (n*log(2)/log(10)) + 3
  34.  
  35. where n=number of bits. 
  36.  
  37. Cheers,
  38. Alun.
  39. -- 
  40. $_='\=*Sxw!jds@j$.jl.dt#Rw%^dcn"K1x(=Bl1nwl!\*1enab^h"F=!J$h%fhcq',
  41. tr&J-ZA-Ij-za-i&A-Za-z&&s&\(&logic&&&s&\*&un&g&s&=&al&g&s&\^&it&g&&
  42. s&%&st&g&&s&\$&ber&g&s&\#&\n&&s&"& of&g,s&([A-Z])& $1&g&&s&\\u&U&&&
  43. s&!&es, &g&s&\\a&A&&s&1&i&g&&print" $_\n";sub liminal{"use perl!";}
  44.